﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Json;

namespace Patent.ToJson
{
    /// <summary>
    /// TableToJson 的摘要说明
    /// </summary>
    public class ToJson
    {
        public ToJson()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        public string TableToJson(DataTable table)
        {
            JsonObjectCollection collection;
            JsonArrayCollection s = new JsonArrayCollection();
            s.Name = "result";

            for (int i = 0; i < table.Rows.Count; i++)
            {
                collection = new JsonObjectCollection();
                for (int j = 0; j < table.Columns.Count; j++)
                {
                    collection.Add(new JsonStringValue(table.Columns[j].ColumnName, table.Rows[i][j].ToString()));
                }
                s.Insert(s.Count, collection);
            }
            return "{" + s.ToString() + "}";
        }
    }    

}